import Promise from '../../utils/es6-promise.min.js'
//获取应用实例
var app = getApp()
Page({
  data: {
    note:[
      
    ],
    showGift: false, //是否显示进店有礼
    showSteps: false,//是否显示关注公众号的引导页
    active: 0
  },
  banner_click(e){
    this.setData({
      active:e.target.dataset.id
    })
  },
  search(){
    wx.navigateTo({
      url: './search',
    })
  },
  closeMsk(e) {
    console.log(e.target.dataset.name)
    if (e.target.dataset.name == 'msk'||e=="close") {
      this.setData({
        showGift: false
      })
    }
  },
  getData(create_time, follow){
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    let baseUrl = getApp().globalData.baseUrl,
        access_token = getApp().globalData.access_token,
        self = this
    
    wx.request({
      url: baseUrl + '/Note/index',
      data: {
        access_token,
        create_time: create_time ? create_time:'',
        follow: follow ? follow:''
      },
      method: 'GET',
      success: data => {
        wx.hideLoading()
        console.log(data.data.data.storeInfo)
        self.setData({
          data: data.data.data.noteInfo,
          card: data.data.data.card,
          storeInfo: data.data.data.storeInfo
        })
        self.computedHeight(data.data.data.noteInfo)

        // 进店有礼
        console.log(data.data.data.noteInfo)
        let card_length = 0
        for (let i in data.data.data.card) {
          card_length++
        }
        console.log('card_length的值为', card_length);
        if (card_length > 0) {
          self.setData({
            showGift: true
          })
        }
      }
    })
  },
  // 根据搞高度重新计算
  computedHeight(left){
    let self = this
    let width = wx.getSystemInfoSync().screenWidth
    let left_height = 0 ,right_height = 0
    let imgHeight = function(url){
      return new Promise( (resolve,reject) => {
        wx.getImageInfo({
          src: url,
          success: res => {
            resolve(res)
          }
        })
      })
    }
    let promise = left.map( (value,index) => {
     return imgHeight(value.images[0])
    })
    Promise.all(promise).then( res => {
        res.forEach( (value,index) => {
          if(index%2==0){
            left_height += (width / 2 / value.width * value.height)
          }else{
            right_height += (width / 2 / value.width * value.height)
          }
        })
        if (Math.abs(right_height - left_height) > 300){
          if (right_height > left_height){
            self.setData({
              add: 'left'
            })
          }else{
            self.setData({
              add:'right'
            })
          }
          console.log(self.data.add)
        }else{
          self.setData({
            add: null
          })
        }
    })
  },
  // 排序
  orderBy(e){
    console.log(e.currentTarget.dataset.id)
    let id = e.currentTarget.dataset.id
    if(id == '0'){
      this.getData()
    }else{
      this.getData('','desc')
    }
  },
  // 领取优惠券
  getCoupon() {
    wx.showLoading({
      title: '正在领取...',
      mask: true
    })
    // console.log(this.data.data)
    let baseUrl = getApp().globalData.baseUrl,
      access_token = getApp().globalData.access_token,
      card_id = this.data.card.id,
      header = getApp().globalData.header,
      self = this

    wx.request({
      url: baseUrl + '/Card/get_coupons',
      method: 'GET',
      data: { access_token, card_id },
      success: data => {
        // console.log(data.data)
        wx.hideLoading();
        if (data.data.code == 200) {
          wx.showToast({
            title: '领取成功！'
          })
          self.setData({
            showGift: false
          })
        }else{
          wx.showToast({
            title: data.data.message
          })
        }
      }
    })
  },
  onLoad(){
    let self = this
    if (!getApp().globalData.access_token){
      getApp().setUser(function(){
        self.getData()
      })
    }else{
      self.getData()
    }
  },
  onPullDownRefresh(){
    this.getData();
    wx.stopPullDownRefresh();
  },
  onShow() {
    this.getData();
  },
  showConcernUs() {
    this.setData({
      showSteps: true
    })
  },
  closeConcernUs(){
    this.setData({
      showSteps: false
    })
  },
})
